﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Threading.Tasks;

namespace DemoOkami
{
	class tabWS
	{
		Context MainContext;
		public tabWS(Context MainContext)
		{
			this.MainContext = MainContext;
		}

		public void GetTab(ref GenericFragmentPagerAdaptor adaptor,ProgressDialog progress)
		{
			string strResult = string.Empty;
			adaptor.AddFragmentView((i, v, b) =>
				{
					var view = i.Inflate(Resource.Layout.WS, v, false);
					var txtv = view.FindViewById<TextView>(Resource.Id.txtv1);
					var btn = view.FindViewById<Button>(Resource.Id.button1);
					btn.Text = "Invocar WS";


					btn.Click += delegate{

						// show the loading overlay on the UI thread
						progress = ProgressDialog.Show(MainContext, "Loading", "Please Wait...", true); 
						//txtv1.Text="Hello from taskA.";
						// spin up a new thread to do some long running work using StartNew
						Task.Factory.StartNew (
							// tasks allow you to use the lambda syntax to pass work
							() => {
								//Begin the task
								strResult=RunWS();
							}

						).ContinueWith ( //Do something whene finish the task
							t => {
								if (progress != null)
									progress.Hide();

								//Show the result at the end of the task
								txtv.Text=strResult;

							}, TaskScheduler.FromCurrentSynchronizationContext()
						);

						// Output a message from the original thread. note that this executes before
						// the background thread has finished.
						//Console.WriteLine("Hello from the calling thread.");
						//txtv1.Text="Hello from the calling thread.";

					};
					//return AddFragmentView
					return view;
				}
			);
		}

		/// <summary>
		/// Run webService
		/// This is a webservice 2.0 not a wcf; something is diffrent
		/// </summary>
		/// <param name="seconds">Seconds.</param>
		protected string RunWS ()
		{
			wsConvert.TempConvert ws = new wsConvert.TempConvert ();
			string strReturn = ws.CelsiusToFahrenheit ("21");
			return strReturn;
		}
	}
}

